package com.example.widget_lib.widget.animator;

import com.example.widget_lib.utils.PxUtil;
import com.example.widget_lib.utils.Util;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.PixelMapHolder;
import ohos.agp.render.ThreeDimView;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;
import ohos.media.image.common.Size;

import java.io.InputStream;

public class BmpOverturnView extends Component  implements Component.DrawTask {

    HiLogLabel TAG = new HiLogLabel(HiLog.LOG_APP,0,"ljkBmpOverturnView");
    public static final int IMAGE_WIDTH = (int) PxUtil.vp2px(180);
    Paint mPaint = new Paint();//转化为setAntiAlias
    PixelMap image;
    ThreeDimView mCamera = new ThreeDimView();
    double left = 0F, top = 0F;
    double topFlip = 0F;
    double bottomFlip = 0F;
    double flipRotation = 0F;


    public BmpOverturnView(Context context, AttrSet attrSet) {
        super(context, attrSet);
        init();
        addDrawTask(this);
    }

    // 将属性名单独包装下，便于外部和谐引用
    public String getPropIsTopFlip() {
        return "topFlip";
    }

    public String getPropIsBottomFlip() {
        return "bottomFlip";
    }

    public String getPropFlipRotation() {
        return "flipRotation";
    }

    public double getTopFlip() {
        return topFlip;
    }

    public void setTopFlip(double topFlip) {
        this.topFlip = topFlip;
        invalidate();
    }

    public double getBottomFlip() {
        return bottomFlip;
    }

    public void setBottomFlip(double bottomFlip) {
        this.bottomFlip = bottomFlip;
        invalidate();
    }

    public double getFlipRotation() {
        return flipRotation;
    }

    public void setFlipRotation(double flipRotation) {
        this.flipRotation = flipRotation;
        invalidate();
    }

    public void propReset() {
        setBottomFlip(0f);
        setTopFlip(0f);
        setFlipRotation(0f);
        invalidate();
    }


    public void init(){
        image = Util.getAvatar(this, (int) (IMAGE_WIDTH*1.5f));
//        image = getPixelMap(ResourceTable.Media_hb);
//        mCamera.setLocation(0, 0, Util.getZForCamera(context));

        mPaint.setAntiAlias(true);
        setBindStateChangedListener(new BindStateChangedListener() {
            @Override
            public void onComponentBoundToWindow(Component component) {
                left = (getWidth()- IMAGE_WIDTH) / (double)2;
                top = (getHeight() - IMAGE_WIDTH) / (double)2 - PxUtil.vp2px(50);

                HiLog.error(TAG,"ljk onSizeChanged= left "+left+"  top="+top);
                HiLog.error(TAG,"ljk onSizeChanged= getWidth() "+getComponentSize().getSizeXToInt()+"  getHeight()="+getComponentSize().getSizeYToInt());
            }

            @Override
            public void onComponentUnboundFromWindow(Component component) {

            }
        });
    }

    private PixelMap getPixelMap(int resID) {
        InputStream drawableInputStream = null;
        try {
            drawableInputStream = getResourceManager().getResource(resID);
            ImageSource.SourceOptions sourceOptions = new ImageSource.SourceOptions();
            sourceOptions.formatHint = "image/jpg";
            ImageSource imageSource = ImageSource.create(drawableInputStream,null);
            ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
            decodingOptions.desiredSize = new Size(600, 600);
//            decodingOptions.desiredRegion = new Rect(10, 10, 1000, 1000);
            decodingOptions.desiredPixelFormat = PixelFormat.ARGB_8888;
            PixelMap pixelMap = imageSource.createPixelmap(decodingOptions);
            return pixelMap;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try{
                if(drawableInputStream!=null){
                    drawableInputStream.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    public void addDrawTask(DrawTask task) {
        super.addDrawTask(task);
        task.onDraw(this, mCanvasForTaskOverContent);
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        HiLog.error(TAG,"ljk onDraw===============left="+left+"  top="+top+"   flipRotation="+flipRotation+"   topFlip="+topFlip);
        HiLog.error(TAG,"ljk onDraw===============getWidth="+getWidth()+"  getHeight="+getHeight());
        // 上半部分
        canvas.save();
        canvas.translate((float) (left + IMAGE_WIDTH / 2), (float) (top + IMAGE_WIDTH /2));
        canvas.rotate(-(float)flipRotation,0,0);
        //z侧的文档有applyToCanvas，但是使用报错
        //z侧没有save、restore方法
        //mCamera.save();
        canvas.save();
        mCamera.rotateX((float)topFlip);
        mCamera.applyToCanvas(canvas);
        canvas.restore();
//        mCamera.restore();
        canvas.clipRect(new RectFloat(-IMAGE_WIDTH, -IMAGE_WIDTH, IMAGE_WIDTH, 0));
        canvas.rotate((float)flipRotation,0,0);
        canvas.translate(-(float)(left + IMAGE_WIDTH / 2), -(float)(top + IMAGE_WIDTH / 2));
        canvas.drawPixelMapHolder(new PixelMapHolder(image), (float)left, (float)top, mPaint);
        canvas.restore();

        // 下半部分
        canvas.save();
        canvas.translate((float)(left + IMAGE_WIDTH / 2), (float)(top + IMAGE_WIDTH / 2));
        canvas.rotate(-(float)flipRotation,0,0);
//        mCamera.save();
        canvas.save();
        mCamera.rotateX((float)bottomFlip);
        mCamera.applyToCanvas(canvas);
        canvas.restore();
//        mCamera.restore();
        canvas.clipRect(new RectFloat( -IMAGE_WIDTH, 0, IMAGE_WIDTH, IMAGE_WIDTH));
        canvas.rotate((float)flipRotation,0,0);
        canvas.translate(-(float)(left + IMAGE_WIDTH / 2), -(float)(top + IMAGE_WIDTH / 2));
        canvas.drawPixelMapHolder(new PixelMapHolder(image), (float)left, (float)top, mPaint);
        canvas.restore();
    }

}
